package com.breeze.blockchain.common.init;

import cn.hutool.core.collection.CollUtil;
import com.breeze.blockchain.mybatis.entity.SettingDo;
import com.breeze.blockchain.service.SettingService;
import com.breeze.components.mybatis.page.AbstractPageQuery;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * 初始化配置,put进redis
 *
 * @author breeze
 */
@Component
public class SysSettingRunner implements ApplicationRunner {

  @Resource SettingService settingService;

  @Override
  public void run(ApplicationArguments args) {
    int i = 1;
    AbstractPageQuery<SettingDo> pageQuery = new AbstractPageQuery<>();
    for (; ; ) {
      pageQuery.setPageNum(i++);
      pageQuery.setPageSize(100);
      Page<SettingDo> page =
          settingService.queryChain().orderBy(SettingDo::getId).desc().page(pageQuery.toPage());
      if (CollUtil.isEmpty(page.getRecords())) {
        break;
      }
      page.getRecords().forEach(s -> settingService.putCache(s));
    }
  }
}
